<?php
include_once "topo.php";
if(logado()){
    if($_SESSION["ci_user_nivel"] == 2){ header("location:index.php"); }
    $status = new status();
    
    if(valida::numero($_GET["id"])){
        $id = $_GET["id"];
        $status->carregar($id);
    }
    
    if($_POST["btn_salvar"] == "Salvar"){
        $erro = 0;
        
        if(!$status->set_descricao($_POST["descricao"])){ $erro = 1; }
        if(!$status->set_email($_POST["email"])){ $erro = 1; }
        
        if($erro == 0){
            $id_status = $status->salvar();
            if($id != ''){
                $log = new log('ci_status', $id_status, "O usiário ".$_SESSION["ci_user_login"]." alterou o registro");
                $log->salvar();
                unset($log);
            }else{
                $log = new log('ci_status', $id_status, "O usiário ".$_SESSION["ci_user_login"]." criou o registro");
                $log->salvar();
                unset($log);
            }
            header("location: status.php");
        }
    }
    
    if($id != ''){
        echo "
        <h3>Novo status</h3>
        ";
    }else{
        echo "
        <h3>Alterar status</h3>
        ";
    }
    ?>
        <form action="" method="post">
            <?php
            if($id == ''){
                echo "ID: ".$status->get_id()."<br><br>";
            }
            ?>
            Descrição<br>
            <input type="text" name="descricao" value="<?=$status->get_descricao()?>"><br><br>
            E-Mail para notificação<br>
            <input type="email" name="email" value="<?=$status->get_email()?>"><br><br>
            <button type="submit" name="btn_salvar" value="Salvar">Salvar</button>
        </form>
    <?php
    
    unset($status);
    
}else{
    header("location: login.php");
}
include_once "rodape.php";
?>
